<?php

namespace App\Services\MiniprogramToGame\HotSearch;

use App\Services\ServiceBase;
use App\Structs\HotSearchStruct;
use Illuminate\Support\Facades\Http;

class Baidu extends ServiceBase
{
    public function reptile()
    {
        $url = 'https://top.baidu.com/board?tab=realtime';
        $response = Http::get($url);
        if (!$response->ok()) {
            return false;
        }

        $body = $response->body();
        $body = str_replace(["\n", "\r", " "], '', $body);
        preg_match('/<!--s-data:(.*?)-->/', $body, $preg);
        $data = json_decode($preg[1], true);

        $list = [];
        foreach ($data['data']['cards'] as $val) {
            foreach ($val['content'] as $index => $item) {
                $class           = new HotSearchStruct;
                $class->title    = $item['word'];
                $class->desc     = $item['desc'];
                $class->hotIndex = $index;
                $class->hotValue = $item['hotScore'];
                $class->url      = $item['url'];
                $class->img      = $item['img'] ?? '';
                if ($index === 0) {
                    $class->isTop = 1;
                }
                $list[] = $class;
            }
        }

        return $list;
    }
}
